package com.sdp.th.controller;


import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.entity.SysEmp;
import com.sdp.th.db.th_eq_alarm_log.service.IThEqAlarmLogService;
import com.sdp.th.entity.ThEqAlarmLog;
import com.sdp.th.utils.CurrentLoginEmp;
import com.sdp.th.utils.ThConstants;

/**
 * <p>
 * 设备报警日志信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2025-04-13
 */
@RestController
@RequestMapping("/ThEqAlarmLogController")
public class ThEqAlarmLogController {

	@Autowired
	private IThEqAlarmLogService iThEqAlarmLogService;

	@Autowired
	private Query query;
	
	@Autowired
	private CurrentLoginEmp CurrentLoginEmp;

	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iThEqAlarmLogService, ThEqAlarmLog.class, dataCenter));
	}
	
	@PostMapping(value = "/alramHandle")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> alramHandle(@RequestBody ThEqAlarmLog alarmLog) {
		if(alarmLog==null || StringUtils.isEmpty(alarmLog.getKeyId())) {
			return Result.error("报警处理信息为空");
		}
		if(StringUtils.isEmpty(alarmLog.getActionType())) {
			return Result.error("处理类型不能为空");
		}
		
		//查询此报警是否已处理过
		ThEqAlarmLog dbLog = iThEqAlarmLogService.getById(alarmLog.getKeyId());
		if(dbLog==null) {
			return Result.error("要处理的报警信息已不存在");
		}
		if(ThConstants.报警处理_已处理.getCode().equals(dbLog.getAlarmAction())) {
			return Result.error("此报警已处理过");
		}
		
		SysEmp emp = CurrentLoginEmp.getCurrentLoginInfo();
		alarmLog.setAlarmAction(ThConstants.报警处理_已处理.getCode());
		alarmLog.setActionTime(LocalDateTime.now());
		alarmLog.setActionEmpNo(emp.getEmpNo());
		alarmLog.setActionEmpName(emp.getEmpName());
		iThEqAlarmLogService.updateById(alarmLog);
		return Result.ok();
	}
	
	@GetMapping(value="batchHandle")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> batchDel(@RequestParam(name="keyIds",required=true) List<String> keyIds) {
		if(keyIds==null || keyIds.size()==0) {
			return Result.error("请先选择要处理的记录");
		}
		SysEmp emp = CurrentLoginEmp.getCurrentLoginInfo();
		LambdaUpdateWrapper<ThEqAlarmLog> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.in(ThEqAlarmLog::getKeyId, keyIds);
		lambdaUpdateWrapper.set(ThEqAlarmLog::getAlarmAction, ThConstants.报警处理_已处理.getCode());
		lambdaUpdateWrapper.set(ThEqAlarmLog::getActionTime, LocalDateTime.now());
		lambdaUpdateWrapper.set(ThEqAlarmLog::getActionEmpNo, emp.getEmpNo());
		lambdaUpdateWrapper.set(ThEqAlarmLog::getActionEmpName, emp.getEmpName());
		iThEqAlarmLogService.update(lambdaUpdateWrapper);
		
		return Result.ok();
	}
	
}

